# Contents: 
# Purpose:
# Status:

require 'Song'
require 'File_Manager'

class Library
	def initialize(filename)
		@my_library = Array.new
		@my_library = File_Manager.load_file(filename)
	end
  
	def add_song(a)
		@my_library << a #stick a into the library
	end
  
	def print_library_debug
		@my_library.each do |s|
			print s.to_response
      			print "\r\n"
		end
	end

	def each
		@my_library.each do |s|
			return s
		end
	end

	def get_song(mhash)
		@my_library.each do |s|
		 	if (s.hash == mhash) then
				return s
			end
		end
		return nil
	end
	def update_song(mhash, rating, playcount, lastplayed,msong)
		flag = 0
		i = 0
		print("Debug: #{mhash}\n#{msong.to_s}")
		while (i<@my_library.length)
			if (@my_library[i].hash == mhash)
				print("Debug: #{@my_library[i].to_s}\n")
				flag=1
				if (rating!=0)
					@my_library[i].set_rating(rating)
				end
				if (playcount != 0)
					@my_library[i].increment_playcount(playcount)
				end
				if (lastplayed != 0)
					@my_library[i].set_last_played(Time.now.strftime("%Y%m%d %H:%M:%S"))
				end
				break
			end
			i=i+1
		end
#		@my_library.each do |s|
#			if (s.hash == mhash)
#				flag = 1
#				if (rating!=0)
#					s.set_rating(rating)
#				end
#				if (playcount != 0)
#					s.increment_playcount(playcount)
#				end
#				if (lastplayed != 0)
#					s.set_last_played(Time.now.strftime("%Y%m%d %H:%M:%S"))
#				
#				end
 # 			break
#			end
#		end
		if flag == 0 then
			add_song(msong)	
		end
	end

	def update_song_data(msong)
		flag = 0
		i = 0
		while (i<@my_library.length)
			if (@my_library[i].hash == msong.hash)
				flag=1

				#filepath,artist,album,title,duration,hash,playcount,rating,last_played
				if (msong.filepath!="" and msong.filepath!="------")
					@my_library[i].set_filepath(msong.filepath)
				end
				if (msong.artist !="" and msong.artist != "------")
					@my_library[i].set_artist(msong.artist)
				end
				if (msong.album !="" and msong.album != "------")
					@my_library[i].set_album(msong.album)
				end
				if (msong.title !="" and msong.title != "------")
					@my_library[i].set_title(msong.title)
				end
				if (msong.duration !="" and msong.duration != "------")
					@my_library[i].set_duration(msong.duration)
				end
				if (msong.rating !="" and msong.rating != "------")
					@my_library[i].set_rating(msong.rating)
				end

				if (msong.playcount!="" and msong.playcount.to_i != 0)
					#@my_library[i].increase_playcount(msong.playcount)
					@my_library[i].set_playcount(msong.playcount)
				end
				
				#if (msong.last_played != 0) and (msong.last_played > @my_library[i].last_played)
				#	@my_library[i].set_last_played(Time.now.strftime("%Y%m%d %H:%M:%S"))
				#end
				@my_library[i].set_last_played(msong.last_played)
				break
			end
			i=i+1
		end

		if flag == 0 then
			add_song(msong)	
		end
	end	

	def import_song_data(msong)
		flag = 0
		i = 0
		while (i<@my_library.length)
			if (@my_library[i].hash == msong.hash)
				flag=1

				#filepath,artist,album,title,duration,hash,playcount,rating,last_played
				@my_library[i].set_filepath(msong.filepath)
				@my_library[i].set_artist(msong.artist)
				@my_library[i].set_album(msong.album)
				@my_library[i].set_title(msong.title)
				@my_library[i].set_duration(msong.duration)
				@my_library[i].set_rating(msong.rating)

				if (msong.playcount!="" and msong.playcount.to_i != 0)
					@my_library[i].set_playcount(msong.playcount)
				end
				
				#if (msong.last_played != 0) and (msong.last_played > @my_library[i].last_played)
				#	@my_library[i].set_last_played(Time.now.strftime("%Y%m%d %H:%M:%S"))
				#end
				@my_library[i].set_last_played(msong.last_played)
				break
			end
			i=i+1
		end

		if flag == 0 then
			add_song(msong)	
		end
	end	
	def save(filename)
		File_Manager.save_file(@my_library, filename)
	end
end
